﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CSmcCs;

namespace Ctlio
{
	/// <summary>
	/// Summary description for Ctlio.
	/// </summary>
	public class Ctlio : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtDeviceName;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ComboBox cboTargetAxis;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label20;
		private System.Windows.Forms.Label label21;
		private System.Windows.Forms.Label label22;
		private System.Windows.Forms.Label label23;
		private System.Windows.Forms.Label label24;
		private System.Windows.Forms.Label label25;
		private System.Windows.Forms.Label label26;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button btnComOut1;
		private System.Windows.Forms.Button btnComOut2;
		private System.Windows.Forms.Button btnComOut3;
		private System.Windows.Forms.Button btnComHoldOff;
		private System.Windows.Forms.Label lblOut1;
		private System.Windows.Forms.Label lblOut2;
		private System.Windows.Forms.Label lblOut3;
		private System.Windows.Forms.Label lblHoldOff;
		private System.Windows.Forms.Label lblIn1;
		private System.Windows.Forms.Label lblIn2;
		private System.Windows.Forms.Label lblIn3;
		private System.Windows.Forms.Label lblIn4;
		private System.Windows.Forms.Label lblIn5;
		private System.Windows.Forms.Label lblIn6;
		private System.Windows.Forms.Label lblIn7;
		private System.Windows.Forms.Button btnComErc;
		private System.Windows.Forms.Button btnComErcClear;
		private System.Windows.Forms.Button btnComAlarmClear;
		private System.Windows.Forms.Button btnExit;
		private	System.Windows.Forms.Timer timer;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Label lblComment;

		// SMC access value
		int			dwRet;
		short		OutData;
		short		InData;
		short		Id;
		short		AxisNo;
		short		ErcOn;
		short		LimitMask;
		string		ErrorString;

		CSmc		Smc	= new CSmc();

		public Ctlio()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.label1 = new System.Windows.Forms.Label();
			this.txtDeviceName = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.cboTargetAxis = new System.Windows.Forms.ComboBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label26 = new System.Windows.Forms.Label();
			this.label25 = new System.Windows.Forms.Label();
			this.label24 = new System.Windows.Forms.Label();
			this.label23 = new System.Windows.Forms.Label();
			this.label22 = new System.Windows.Forms.Label();
			this.label21 = new System.Windows.Forms.Label();
			this.label20 = new System.Windows.Forms.Label();
			this.lblIn7 = new System.Windows.Forms.Label();
			this.lblIn6 = new System.Windows.Forms.Label();
			this.lblIn5 = new System.Windows.Forms.Label();
			this.lblIn4 = new System.Windows.Forms.Label();
			this.lblIn3 = new System.Windows.Forms.Label();
			this.lblIn2 = new System.Windows.Forms.Label();
			this.lblIn1 = new System.Windows.Forms.Label();
			this.lblHoldOff = new System.Windows.Forms.Label();
			this.lblOut3 = new System.Windows.Forms.Label();
			this.lblOut2 = new System.Windows.Forms.Label();
			this.lblOut1 = new System.Windows.Forms.Label();
			this.btnComHoldOff = new System.Windows.Forms.Button();
			this.btnComOut3 = new System.Windows.Forms.Button();
			this.btnComOut2 = new System.Windows.Forms.Button();
			this.btnComOut1 = new System.Windows.Forms.Button();
			this.label8 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.btnComErc = new System.Windows.Forms.Button();
			this.btnComErcClear = new System.Windows.Forms.Button();
			this.btnComAlarmClear = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.lblComment = new System.Windows.Forms.Label();
			this.btnExit = new System.Windows.Forms.Button();
			this.timer = new System.Windows.Forms.Timer(this.components);
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(79, 14);
			this.label1.TabIndex = 0;
			this.label1.Text = "Device Name";
			// 
			// txtDeviceName
			// 
			this.txtDeviceName.Location = new System.Drawing.Point(112, 13);
			this.txtDeviceName.Name = "txtDeviceName";
			this.txtDeviceName.Size = new System.Drawing.Size(112, 21);
			this.txtDeviceName.TabIndex = 2;
			this.txtDeviceName.Text = "";
			this.txtDeviceName.TextChanged += new System.EventHandler(this.txtDeviceName_TextChanged);
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(264, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(44, 14);
			this.label2.TabIndex = 2;
			this.label2.Text = "AxisNo";
			// 
			// cboTargetAxis
			// 
			this.cboTargetAxis.Location = new System.Drawing.Point(320, 12);
			this.cboTargetAxis.Name = "cboTargetAxis";
			this.cboTargetAxis.Size = new System.Drawing.Size(72, 23);
			this.cboTargetAxis.Sorted = true;
			this.cboTargetAxis.TabIndex = 3;
			this.cboTargetAxis.SelectedIndexChanged += new System.EventHandler(this.cboTargetAxis_SelectedIndexChanged);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label26,
																					this.label25,
																					this.label24,
																					this.label23,
																					this.label22,
																					this.label21,
																					this.label20,
																					this.lblIn7,
																					this.lblIn6,
																					this.lblIn5,
																					this.lblIn4,
																					this.lblIn3,
																					this.lblIn2,
																					this.lblIn1,
																					this.lblHoldOff,
																					this.lblOut3,
																					this.lblOut2,
																					this.lblOut1,
																					this.btnComHoldOff,
																					this.btnComOut3,
																					this.btnComOut2,
																					this.btnComOut1,
																					this.label8,
																					this.label7,
																					this.label6,
																					this.label5,
																					this.label4,
																					this.label3});
			this.groupBox1.Location = new System.Drawing.Point(8, 40);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(440, 184);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Control I/O";
			// 
			// label26
			// 
			this.label26.AutoSize = true;
			this.label26.Location = new System.Drawing.Point(392, 120);
			this.label26.Name = "label26";
			this.label26.Size = new System.Drawing.Size(24, 14);
			this.label26.TabIndex = 28;
			this.label26.Text = "IN7";
			// 
			// label25
			// 
			this.label25.AutoSize = true;
			this.label25.Location = new System.Drawing.Point(344, 120);
			this.label25.Name = "label25";
			this.label25.Size = new System.Drawing.Size(24, 14);
			this.label25.TabIndex = 27;
			this.label25.Text = "IN6";
			// 
			// label24
			// 
			this.label24.AutoSize = true;
			this.label24.Location = new System.Drawing.Point(296, 120);
			this.label24.Name = "label24";
			this.label24.Size = new System.Drawing.Size(24, 14);
			this.label24.TabIndex = 26;
			this.label24.Text = "IN5";
			// 
			// label23
			// 
			this.label23.AutoSize = true;
			this.label23.Location = new System.Drawing.Point(248, 120);
			this.label23.Name = "label23";
			this.label23.Size = new System.Drawing.Size(24, 14);
			this.label23.TabIndex = 25;
			this.label23.Text = "IN4";
			// 
			// label22
			// 
			this.label22.AutoSize = true;
			this.label22.Location = new System.Drawing.Point(200, 120);
			this.label22.Name = "label22";
			this.label22.Size = new System.Drawing.Size(24, 14);
			this.label22.TabIndex = 24;
			this.label22.Text = "IN3";
			// 
			// label21
			// 
			this.label21.AutoSize = true;
			this.label21.Location = new System.Drawing.Point(152, 120);
			this.label21.Name = "label21";
			this.label21.Size = new System.Drawing.Size(24, 14);
			this.label21.TabIndex = 23;
			this.label21.Text = "IN2";
			// 
			// label20
			// 
			this.label20.AutoSize = true;
			this.label20.Location = new System.Drawing.Point(104, 120);
			this.label20.Name = "label20";
			this.label20.Size = new System.Drawing.Size(24, 14);
			this.label20.TabIndex = 22;
			this.label20.Text = "IN1";
			// 
			// lblIn7
			// 
			this.lblIn7.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblIn7.Location = new System.Drawing.Point(392, 144);
			this.lblIn7.Name = "lblIn7";
			this.lblIn7.Size = new System.Drawing.Size(35, 21);
			this.lblIn7.TabIndex = 21;
			this.lblIn7.Text = "OUT1";
			// 
			// lblIn6
			// 
			this.lblIn6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblIn6.Location = new System.Drawing.Point(344, 144);
			this.lblIn6.Name = "lblIn6";
			this.lblIn6.Size = new System.Drawing.Size(35, 21);
			this.lblIn6.TabIndex = 20;
			this.lblIn6.Text = "OUT1";
			// 
			// lblIn5
			// 
			this.lblIn5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblIn5.Location = new System.Drawing.Point(296, 144);
			this.lblIn5.Name = "lblIn5";
			this.lblIn5.Size = new System.Drawing.Size(35, 21);
			this.lblIn5.TabIndex = 19;
			this.lblIn5.Text = "OUT1";
			// 
			// lblIn4
			// 
			this.lblIn4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblIn4.Location = new System.Drawing.Point(248, 144);
			this.lblIn4.Name = "lblIn4";
			this.lblIn4.Size = new System.Drawing.Size(35, 21);
			this.lblIn4.TabIndex = 18;
			this.lblIn4.Text = "OUT1";
			// 
			// lblIn3
			// 
			this.lblIn3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblIn3.Location = new System.Drawing.Point(200, 144);
			this.lblIn3.Name = "lblIn3";
			this.lblIn3.Size = new System.Drawing.Size(35, 21);
			this.lblIn3.TabIndex = 17;
			this.lblIn3.Text = "OUT1";
			// 
			// lblIn2
			// 
			this.lblIn2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblIn2.Location = new System.Drawing.Point(152, 144);
			this.lblIn2.Name = "lblIn2";
			this.lblIn2.Size = new System.Drawing.Size(35, 21);
			this.lblIn2.TabIndex = 16;
			this.lblIn2.Text = "OUT1";
			// 
			// lblIn1
			// 
			this.lblIn1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblIn1.Location = new System.Drawing.Point(104, 144);
			this.lblIn1.Name = "lblIn1";
			this.lblIn1.Size = new System.Drawing.Size(35, 21);
			this.lblIn1.TabIndex = 15;
			this.lblIn1.Text = "OUT1";
			// 
			// lblHoldOff
			// 
			this.lblHoldOff.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblHoldOff.Location = new System.Drawing.Point(352, 81);
			this.lblHoldOff.Name = "lblHoldOff";
			this.lblHoldOff.Size = new System.Drawing.Size(69, 21);
			this.lblHoldOff.TabIndex = 14;
			this.lblHoldOff.Text = "Hold Off";
			// 
			// lblOut3
			// 
			this.lblOut3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblOut3.Location = new System.Drawing.Point(272, 81);
			this.lblOut3.Name = "lblOut3";
			this.lblOut3.Size = new System.Drawing.Size(69, 21);
			this.lblOut3.TabIndex = 13;
			this.lblOut3.Text = "OUT1";
			// 
			// lblOut2
			// 
			this.lblOut2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblOut2.Location = new System.Drawing.Point(184, 81);
			this.lblOut2.Name = "lblOut2";
			this.lblOut2.Size = new System.Drawing.Size(69, 21);
			this.lblOut2.TabIndex = 12;
			this.lblOut2.Text = "OUT1";
			// 
			// lblOut1
			// 
			this.lblOut1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblOut1.Location = new System.Drawing.Point(104, 81);
			this.lblOut1.Name = "lblOut1";
			this.lblOut1.Size = new System.Drawing.Size(69, 21);
			this.lblOut1.TabIndex = 11;
			this.lblOut1.Text = "OUT1";
			// 
			// btnComHoldOff
			// 
			this.btnComHoldOff.Location = new System.Drawing.Point(352, 42);
			this.btnComHoldOff.Name = "btnComHoldOff";
			this.btnComHoldOff.Size = new System.Drawing.Size(53, 29);
			this.btnComHoldOff.TabIndex = 10;
			this.btnComHoldOff.Click += new System.EventHandler(this.btnComHoldOff_Click);
			// 
			// btnComOut3
			// 
			this.btnComOut3.Location = new System.Drawing.Point(272, 42);
			this.btnComOut3.Name = "btnComOut3";
			this.btnComOut3.Size = new System.Drawing.Size(53, 29);
			this.btnComOut3.TabIndex = 9;
			this.btnComOut3.Click += new System.EventHandler(this.btnComOut3_Click);
			// 
			// btnComOut2
			// 
			this.btnComOut2.Location = new System.Drawing.Point(184, 42);
			this.btnComOut2.Name = "btnComOut2";
			this.btnComOut2.Size = new System.Drawing.Size(53, 29);
			this.btnComOut2.TabIndex = 8;
			this.btnComOut2.Click += new System.EventHandler(this.btnComOut2_Click);
			// 
			// btnComOut1
			// 
			this.btnComOut1.Location = new System.Drawing.Point(104, 42);
			this.btnComOut1.Name = "btnComOut1";
			this.btnComOut1.Size = new System.Drawing.Size(53, 29);
			this.btnComOut1.TabIndex = 7;
			this.btnComOut1.Click += new System.EventHandler(this.btnComOut1_Click);
			// 
			// label8
			// 
			this.label8.AutoSize = true;
			this.label8.Location = new System.Drawing.Point(352, 18);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(50, 14);
			this.label8.TabIndex = 6;
			this.label8.Text = "Hold Off";
			// 
			// label7
			// 
			this.label7.AutoSize = true;
			this.label7.Location = new System.Drawing.Point(272, 18);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(37, 14);
			this.label7.TabIndex = 5;
			this.label7.Text = "OUT3";
			// 
			// label6
			// 
			this.label6.AutoSize = true;
			this.label6.Location = new System.Drawing.Point(184, 18);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(37, 14);
			this.label6.TabIndex = 4;
			this.label6.Text = "OUT2";
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(104, 18);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(37, 14);
			this.label5.TabIndex = 3;
			this.label5.Text = "OUT1";
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(8, 128);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(65, 14);
			this.label4.TabIndex = 2;
			this.label4.Text = "Intput Data";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(8, 40);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(71, 14);
			this.label3.TabIndex = 1;
			this.label3.Text = "Output Data";
			// 
			// btnComErc
			// 
			this.btnComErc.Location = new System.Drawing.Point(8, 232);
			this.btnComErc.Name = "btnComErc";
			this.btnComErc.Size = new System.Drawing.Size(88, 32);
			this.btnComErc.TabIndex = 5;
			this.btnComErc.Text = "Erc OutPut";
			this.btnComErc.Click += new System.EventHandler(this.btnComErc_Click);
			// 
			// btnComErcClear
			// 
			this.btnComErcClear.Location = new System.Drawing.Point(108, 232);
			this.btnComErcClear.Name = "btnComErcClear";
			this.btnComErcClear.Size = new System.Drawing.Size(88, 32);
			this.btnComErcClear.TabIndex = 6;
			this.btnComErcClear.Text = "Erc Reset";
			this.btnComErcClear.Click += new System.EventHandler(this.btnComErcClear_Click);
			// 
			// btnComAlarmClear
			// 
			this.btnComAlarmClear.Location = new System.Drawing.Point(208, 232);
			this.btnComAlarmClear.Name = "btnComAlarmClear";
			this.btnComAlarmClear.Size = new System.Drawing.Size(136, 32);
			this.btnComAlarmClear.TabIndex = 7;
			this.btnComAlarmClear.Text = "Alarm Clear Output";
			this.btnComAlarmClear.Click += new System.EventHandler(this.btnComAlarmClear_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.lblComment});
			this.groupBox2.Location = new System.Drawing.Point(8, 272);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(440, 112);
			this.groupBox2.TabIndex = 8;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "COMMENT";
			// 
			// lblComment
			// 
			this.lblComment.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblComment.Location = new System.Drawing.Point(8, 24);
			this.lblComment.Name = "lblComment";
			this.lblComment.Size = new System.Drawing.Size(424, 80);
			this.lblComment.TabIndex = 0;
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(16, 392);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(88, 32);
			this.btnExit.TabIndex = 1;
			this.btnExit.Text = "EXIT";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// timer
			// 
			this.timer.Tick += new System.EventHandler(this.timer_Tick);
			// 
			// Ctlio
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(456, 430);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnExit,
																		  this.groupBox2,
																		  this.btnComAlarmClear,
																		  this.btnComErcClear,
																		  this.btnComErc,
																		  this.groupBox1,
																		  this.cboTargetAxis,
																		  this.label2,
																		  this.txtDeviceName,
																		  this.label1});
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Ctlio";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Ctlio";
			this.Load += new System.EventHandler(this.Ctlio_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Ctlio());
		}

		private void Ctlio_Load(object sender, System.EventArgs e)
		{
			//-------------
			// Initialize
			//-------------
			AxisNo = 1;

			// AxisNo ComboBox
			string	wStr;
			for(int i=0;i<8;i++)
			{
				wStr = string.Format("{0:d}", i+1 );
				cboTargetAxis.Items.Add(wStr);
			}

			string	DeviceName;
			DeviceName="SMC000";
			txtDeviceName.Text = DeviceName;

			// ComboBox Initialise
			cboTargetAxis.SelectedIndex = AxisNo - 1;

			timer.Enabled = true;		// eventid=10 100msec 
		}

		private void DispComment(string func, string msg) 
		{
			lblComment.Text = string.Format("{0}: {1}", func, msg);
		}

		private void btnComHoldOff_Click(object sender, System.EventArgs e)
		{
			short HoldOff;
			dwRet = Smc.WGetHoldOff(Id, AxisNo, out HoldOff);
			if( (HoldOff & (short)CSmcConst.CSMC_HOLDOFF) == (int)CSmcConst.CSMC_HOLDOFF )
			{
				dwRet = Smc.WSetHoldOff(Id, AxisNo, (short)CSmcConst.CSMC_HOLD);
				if( dwRet != 0 )
				{
					Smc.WGetErrorString(dwRet, out ErrorString);
					DispComment("SmcWSetHoldOff", ErrorString);
					return;
				}
			}
			else
			{
				dwRet = Smc.WSetHoldOff(Id, AxisNo, (short)CSmcConst.CSMC_HOLDOFF);
				if( dwRet != 0 )
				{
					Smc.WGetErrorString(dwRet, out ErrorString);
					DispComment("SmcWSetHoldOff", ErrorString);
					return;
				}
			}
		}

		private void btnComAlarmClear_Click(object sender, System.EventArgs e)
		{
			dwRet = Smc.WSetAlarmClear(Id, AxisNo);	
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWSetAlarmClear", ErrorString);
				return;
			}
		}

		private void btnComOut1_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Set Ctrl Output Data to Driver
			//-----------------------------------
			dwRet = Smc.WGetDigitalOut(Id, AxisNo, out OutData);
			if( (OutData & (short)CSmcConst.CSMC_OUT1) == (int)CSmcConst.CSMC_OUT1 )
			{
				dwRet = Smc.WSetDigitalOut(Id, AxisNo, 0, (short)CSmcConst.CSMC_OUT1);
				if( dwRet != 0 )
				{
					Smc.WGetErrorString(dwRet, out ErrorString);
					DispComment("SmcWSetDigitalOut", ErrorString);
					return;
				}
			}
			else
			{
				dwRet = Smc.WSetDigitalOut(Id, AxisNo, (short)CSmcConst.CSMC_OUT1, (short)CSmcConst.CSMC_OUT1);
				if( dwRet != 0 )
				{
					Smc.WGetErrorString(dwRet, out ErrorString);
					DispComment("SmcWSetDigitalOut", ErrorString);
					return;
				}
			}
		}

		private void btnComOut2_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Set Ctrl Output Data to Driver
			//-----------------------------------
			dwRet = Smc.WGetDigitalOut(Id, AxisNo, out OutData);
			if( (OutData & (short)CSmcConst.CSMC_OUT2) == (int)CSmcConst.CSMC_OUT2 )
			{
				dwRet = Smc.WSetDigitalOut(Id, AxisNo, 0, (short)CSmcConst.CSMC_OUT2);
				if( dwRet != 0 )
				{
					Smc.WGetErrorString(dwRet, out ErrorString);
					DispComment("SmcWSetDigitalOut", ErrorString);
					return;
				}
			}
			else
			{
				dwRet = Smc.WSetDigitalOut(Id, AxisNo, (short)CSmcConst.CSMC_OUT2, (short)CSmcConst.CSMC_OUT2);
				if( dwRet != 0 )
				{
					Smc.WGetErrorString(dwRet, out ErrorString);
					DispComment("SmcWSetDigitalOut", ErrorString);
					return;
				}
			}
		}

		private void btnComOut3_Click(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Set Ctrl Output Data to Driver
			//-----------------------------------
			dwRet = Smc.WGetDigitalOut(Id, AxisNo, out OutData);
			if( (OutData & (short)CSmcConst.CSMC_OUT3) == (int)CSmcConst.CSMC_OUT3 )
			{
				dwRet = Smc.WSetDigitalOut(Id, AxisNo, 0, (short)CSmcConst.CSMC_OUT3);
				if( dwRet != 0 )
				{
					Smc.WGetErrorString(dwRet, out ErrorString);
					DispComment("SmcWSetDigitalOut", ErrorString);
					return;
				}
			}
			else
			{
				dwRet = Smc.WSetDigitalOut(Id, AxisNo, (short)CSmcConst.CSMC_OUT3, (short)CSmcConst.CSMC_OUT3);
				if( dwRet != 0 )
				{
					Smc.WGetErrorString(dwRet, out ErrorString);
					DispComment("SmcWSetDigitalOut", ErrorString);
					return;
				}
			}
		}

		private void timer_Tick(object sender, System.EventArgs e)
		{
			//-------------------------------
			// Get Ctrl Output Data from Driver
			//-------------------------------
			dwRet = Smc.WGetDigitalOut(Id, AxisNo, out OutData);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWGetDigitalOut", ErrorString);
				return;
			}
			if( (OutData & (short)CSmcConst.CSMC_OUT1) == (int)CSmcConst.CSMC_OUT1 )
				lblOut1.Text = "ON";
			else
				lblOut1.Text = "OFF";

			if( (OutData & (short)CSmcConst.CSMC_OUT2) == (int)CSmcConst.CSMC_OUT2 )
				lblOut2.Text = "ON";
			else
				lblOut2.Text = "OFF";


			if( (OutData & (short)CSmcConst.CSMC_OUT3) == (int)CSmcConst.CSMC_OUT3 )
				lblOut3.Text = "ON";
			else
				lblOut3.Text = "OFF";

			//-------------------------------
			// Get Ctrl Input Data from Driver
			//-------------------------------
			dwRet = Smc.WGetDigitalIn(Id, AxisNo, out InData);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWGetDigitalIn", ErrorString);
				return;
			}
			if( (InData & (short)CSmcConst.CSMC_IN1) == (int)CSmcConst.CSMC_IN1 )
				lblIn1.Text = "ON";
			else
				lblIn1.Text = "OFF";

			if( (InData & (short)CSmcConst.CSMC_IN2) == (int)CSmcConst.CSMC_IN2 )
				lblIn2.Text = "ON";
			else
				lblIn2.Text = "OFF";


			if( (InData & (short)CSmcConst.CSMC_IN3) == (int)CSmcConst.CSMC_IN3 )
				lblIn3.Text = "ON";
			else
				lblIn3.Text = "OFF";

			if( (InData & (short)CSmcConst.CSMC_IN4) == (int)CSmcConst.CSMC_IN4 )
				lblIn4.Text = "ON";
			else
				lblIn4.Text = "OFF";

			if( (InData & (short)CSmcConst.CSMC_IN5) == (int)CSmcConst.CSMC_IN5 )
				lblIn5.Text = "ON";
			else
				lblIn5.Text = "OFF";

			if( (InData & (short)CSmcConst.CSMC_IN6) == (int)CSmcConst.CSMC_IN6 )
				lblIn6.Text = "ON";
			else
				lblIn6.Text = "OFF";

			if( (InData & (short)CSmcConst.CSMC_IN7) == (int)CSmcConst.CSMC_IN7 )
				lblIn7.Text = "ON";
			else
				lblIn7.Text = "OFF";

			short HoldOff;
			dwRet = Smc.WGetHoldOff(Id, AxisNo, out HoldOff);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWGetHoldOff", ErrorString);
				return;
			}
			if( (HoldOff & (short)CSmcConst.CSMC_HOLDOFF) == (int)CSmcConst.CSMC_HOLDOFF )
				lblHoldOff.Text = "HOLDOFF";
			else
				lblHoldOff.Text = "HOLD";

			//
			dwRet = Smc.WGetLimitMask(Id, AxisNo, out LimitMask);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWGetLimitMask", ErrorString);
				return;
			}
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			timer.Enabled = false;

			this.Close();
		}

		private void txtDeviceName_TextChanged(object sender, System.EventArgs e)
		{
			string	wStr="OK";
			string	DeviceName;

			if( Id != -1 )
			{
				Smc.WExit(Id);
			}
			DeviceName = txtDeviceName.Text;
			dwRet = Smc.WInit(DeviceName, out Id);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWInit", ErrorString);
				Id=-1;
				return;
			}
			lblComment.Text = wStr;
		}

		private void cboTargetAxis_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	wStr="OK";
			short	InitParam;

			AxisNo = (short)(cboTargetAxis.SelectedIndex+1);
			// InitParam check
			dwRet = Smc.WGetInitParam(Id , AxisNo , out InitParam);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWGetInitParam", ErrorString);
				return;
			}
			if( InitParam == 0 )
			{ // SmcWSetInitParam
				dwRet = Smc.WSetInitParam(Id , AxisNo);
			}
			lblComment.Text = wStr;

		}

		private void btnComErc_Click(object sender, System.EventArgs e)
		{
			ErcOn=0;
			// ERC out
			dwRet = Smc.WSetErcOut(Id, AxisNo, ErcOn);
		}

		private void btnComErcClear_Click(object sender, System.EventArgs e)
		{
			ErcOn = 1;
			dwRet = Smc.WSetErcOut(Id, AxisNo, ErcOn);
		}
	}
}
